import cv2
import numpy as np
import sys

WINDOW_W = 960
WINDOW_H = 720
WINDOW_NAME = "Position Adjustment" 


# 目标方块的边长为3.9
CUBE_WIDTH = 3.9

def nothing(x):
    pass

def run():
    # 打开摄像头，若失败则提示
    cap = cv2.VideoCapture(0)
    if not cap.isOpened():
        print("无法打开摄像头，请检查连接。")
        return
    else:
        print("摄像头成功打开。")

    # 创建窗口并设置大小
    cv2.namedWindow(WINDOW_NAME,cv2.WINDOW_NORMAL)
    cv2.resizeWindow(WINDOW_NAME,WINDOW_W,WINDOW_H)
    cv2.namedWindow(WINDOW_NAME)

    # 设置透明度参数
    alpha = 0.2

    print("按'q'关闭摄像头")
    
    # 主循环，持续读取摄像头画面
    while True:
        ret, frame = cap.read()
        height, width = frame.shape[:2]
        center = (width//2, height//2)

        # 定义感兴趣区域
        inner_region = [(center[0] - 210, 0), (center[0] + 210, center[1] + 90)]
        outer_region = [(center[0] - 270, 0), (center[0] + 270, center[1] + 140)]

        # 创建红色遮罩，覆盖外部区域
        red_outer_mask = np.zeros_like(frame, dtype=np.uint8)
        cv2.rectangle(red_outer_mask, (0, 0), (width, height), (0, 0, 255), -1)
        cv2.rectangle(red_outer_mask, outer_region[0], outer_region[1], (0, 0, 0), -1)  # Cut out the outer region

        # 应用红色遮罩
        cv2.addWeighted(red_outer_mask, alpha, frame, 1 - alpha, 0, frame)

        # 创建红色遮罩，覆盖内部区域
        red_inner_mask = np.zeros_like(frame, dtype=np.uint8)
        cv2.rectangle(red_inner_mask, inner_region[0], inner_region[1], (0, 0, 255), -1)

        # 应用红色遮罩
        cv2.addWeighted(red_inner_mask, alpha, frame, 1 - alpha, 0, frame)

        # 创建绿色遮罩，覆盖内外区域之间
        green_mask = np.zeros_like(frame, dtype=np.uint8)
        cv2.rectangle(green_mask, outer_region[0], outer_region[1], (0, 255, 0), -1)
        cv2.rectangle(green_mask, inner_region[0], inner_region[1], (0, 0, 0), -1)  # Cut out the inner region

        # 应用绿色遮罩
        cv2.addWeighted(green_mask, alpha, frame, 1 - alpha, 0, frame)


        cv2.line(frame, (center[0] - 210, center[1] + 90), (center[0] + 210, center[1] + 90), (0, 255,0))
        cv2.line(frame, (center[0] - 210, 0), (center[0] - 210, center[1] + 90), (0, 255, 0))
        cv2.line(frame, (center[0] + 210, 0), (center[0] + 210, center[1] + 90), (0, 255, 0))

        cv2.line(frame, (center[0] - 270, center[1] + 140), (center[0] + 270, center[1] + 140), (0, 255,0))
        cv2.line(frame, (center[0] - 270, 0), (center[0] - 270, center[1] + 140), (0, 255, 0))
        cv2.line(frame, (center[0] + 270, 0), (center[0] + 270, center[1] + 140), (0, 255, 0))

        cv2.circle(frame, center, 5, (0, 255, 0), -1)  # Green circle with radius 5
        
        # 在图像中心画出一个圆点
        cv2.circle(frame, center, 5, (0, 255, 0), -1)  # Green circle with radius 5

        # 以此点为正方形中心，画出边长分别为 20, 30, 40 像素的方框
        for size in [20, 30, 40]:
            top_left = (center[0] - size // 2, center[1] - size // 2)
            bottom_right = (center[0] + size // 2, center[1] + size // 2)
            cv2.rectangle(frame, top_left, bottom_right, (255, 0, 0), 2)  #  

        # 显示画面
        cv2.imshow(WINDOW_NAME, frame)
        
        # 按下q键退出
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break
    cv2.destroyAllWindows()

    
if __name__ == '__main__':
    run()
